package com.fenghongzhang.day007;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {
    private ViewPager vp;

    private ThreeFragment threeFragment;
    private List<Fragment> list;
    private LinearLayout ll;

    private  List<ImageView> imageViewList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //sp
        SharedPreferences isOpen = getSharedPreferences("isLogin", MODE_PRIVATE);

        boolean isOpen1 = isOpen.getBoolean("isOpen", false);
        if(isOpen1){
            Intent intent = new Intent(this, Main2Activity.class);
            startActivity(intent);
            finish();
            return;
        }else{
            SharedPreferences.Editor edit = isOpen.edit();
            edit.putBoolean("isOpen",true);
            edit.commit();

        }


        vp = (ViewPager) findViewById(R.id.vp);

        ll = (LinearLayout) findViewById(R.id.ll);

         list = new ArrayList<>();
        list.add(new OneFragment());
        list.add(new TwoFragment());
        threeFragment = new ThreeFragment();
        list.add(threeFragment);

        FGAdapter fgAdapter = new FGAdapter(getSupportFragmentManager(),list);
        vp.setAdapter(fgAdapter);


        //添加所有图形到一个数组中
       imageViewList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            ImageView imageView = new ImageView(this);
            if (i == 0) {
                imageView.setImageResource(R.drawable.cirlce_t);
            }else{
                imageView.setImageResource(R.drawable.circle_f);
            }
            imageViewList.add(imageView);
            ll.addView(imageView);
        }



        //自动翻页
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            int index = 0;
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
//                        vp.setCurrentItem(index);
//                        index++;
//                        if(index >= list.size() ){
//                            timer.cancel();
//                        }
                    }
                });
            }
        },0,1000);


        //翻页监听
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                //如果是最后一页,开始倒计时
                if(position == list.size() -1){
                    threeFragment.handler.sendEmptyMessage(110);
                }

                //修改颜色
                for (int i = 0; i < imageViewList.size(); i++) {
                    ImageView imageView = imageViewList.get(i);
                    if(position == i){
                        imageView.setImageResource(R.drawable.cirlce_t);
                    }else{
                        imageView.setImageResource(R.drawable.circle_f);
                    }

                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }
}
